#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_frameListModel(0),
    m_parser(0),
    m_dataPath(""),
    m_plistPath("")
{
    ui->setupUi(this);

    setupDataController();


    //for test
//    if(m_mainController->parsePlistFile("D:/cocos2D/TestTools/plisteditor/Test/Resource/shipAnimation.plist"))
//     {
//             m_mainController->updateFrameInfoToView();
//             m_mainController->recordPlistInfo(filePath);
//              m_plistPath = filePath;
//      }
}

MainWindow::~MainWindow()
{
    CC_SAFE_DELETE_NULL(m_frameListModel);
    CC_SAFE_DELETE_NULL(m_parser);
    CC_SAFE_DELETE_NULL(ui);

    CC_SAFE_DELETE_NULL(m_mainController);
}

void MainWindow::setupDataController()
{
    m_mainController = new MainController();

    m_mainController->setActionListView(ui->ActionList);
    m_mainController->setFrameListView(ui->FrameList);\
    m_mainController->setSourceFrameListView(ui->SourceFramesList);
    m_mainController->setFrameGraphicsView(ui->frameGraphicsView);
}

void MainWindow::loadDataFromFile()
{
    QString filePath = QFileDialog::getOpenFileName(this,tr("load data .."), m_dataPath, tr("animation file (*.xml);;"));
    if(!filePath.isEmpty())
    {
         if(m_mainController->loadDataFromFile(filePath))
         {
              QMessageBox::warning(this, tr("success"),tr("data file has been loaded"));
         }
         else
         {
              QMessageBox::warning(this, tr("error"),tr("save failed"));
         }
    }
}

void MainWindow::saveDataAsFile()
{
    if(m_dataPath != "")
    {
        handleSaveDataToFile(m_dataPath);
        return ;
    }

    QString filePath = QFileDialog::getOpenFileName(this,tr("save to .."), m_dataPath, tr("animation file (*.xml);;"));
    if(!filePath.isEmpty())
    {
        m_dataPath = filePath;
        handleSaveDataToFile(filePath);
    }
}

void MainWindow::handleSaveDataToFile(QString& filePath)
{
    if(m_mainController->saveDataToFile(filePath))
    {
         QMessageBox::warning(this, tr("success"),tr("data file has been saved"));
    }
    else
    {
         QMessageBox::warning(this, tr("error"),tr("save failed"));
    }
}

void MainWindow::addPlistFile()
{
    QString filePath = QFileDialog::getOpenFileName(this,tr("Add Plist file .."), m_plistPath, tr("Plist Files (*.plist);;"));
    if(!filePath.isEmpty())
    {
        if(m_mainController->hasAddPlist(filePath))
        {
            QMessageBox::warning(this, tr("error"),tr("the plist file has been added"));
        }
        else if(m_mainController->parsePlistFile(filePath))
        {
              m_mainController->recordPlistInfo(filePath);

              QMessageBox::warning(this, tr("success "),tr("successfull parse new plist file"));
              m_plistPath = filePath;
        }
        else
        {
            QMessageBox::warning(this, tr("error"),tr("parse plist file fail"));
        }
    }
}

void MainWindow::stopAnimation()
{
    m_mainController->stopAnimation();
}

void MainWindow::playAnimation()
{
    m_mainController->playCurrentAnimation();

}


void MainWindow::on_NewAction_clicked()
{
    m_mainController->createNewAction();
}

void MainWindow::on_DeleteAction_clicked()
{
    m_mainController->deleteAction();
}

void MainWindow::on_addFrame_clicked()
{
    m_mainController->addFrameToAction();
}

void MainWindow::on_removeFrame_clicked()
{
    m_mainController->removeFrame();
}

void MainWindow::on_actionSave_Data_triggered()
{
    saveDataAsFile();
}

void MainWindow::on_actionSave_Data_As_triggered()
{
   saveDataAsFile();
}

void MainWindow::on_actionLoadData_triggered()
{
    loadDataFromFile();
}


void MainWindow::on_actionOpen_Plist_File_triggered()
{
     addPlistFile();
}

void MainWindow::on_actionNew_Data_triggered()
{
    AnimationDataHandler::instance()->resetAll();
}

void MainWindow::on_stopAnimation_clicked()
{
    stopAnimation();
}

void MainWindow::on_playAnimation_clicked()
{
    playAnimation();
}
